<?php

namespace App\Admin\Controllers;

use App\Models\Test_config;
use App\Models\Student;
use App\Models\Squad;
use App\Models\Student_mark;
use Encore\Admin\Auth\Database\Administrator;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\ExcelExpoter;
use Maatwebsite\Excel\Facades\Excel;
use Encore\Admin\Show;
use Illuminate\Http\Request;
use DB;

class Student_mark_viewController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.student_mark'));
            $content->description(trans('admin.list'));

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(trans('admin.student_mark'));
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.student_mark'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Student_mark::class, function (Grid $grid) {
            $squads = Squad::where('class_teacher','=',Admin::user()->id)->pluck('id')->toArray();

            $grid->model()->wherein('squad_id', $squads);
            $grid->model()->orderBy('id', 'desc');
            $grid->id(trans('admin.ID'))->sortable();
            $grid->squad_id(trans('admin.squad_id'))->display(function($squad_id){
                return Squad::find($this->squad_id)->name;
            })->sortable();
            $grid->student_id(trans('admin.en_name'))->display(function($student_id){
                return Student::find($this->student_id)->en_name;
            })->sortable();
            $grid->test_id(trans('admin.test_name'))->display(function($test_id){
                return Test_config::find($this->test_id)->name;
            })->sortable();
            $grid->test_batch(trans('admin.test_batch'))->sortable();
            $grid->test_score(trans('admin.test_score'))->editable();
            $grid->expandFilter();
            $grid->filter(function ($filter) {
                $filter->column(1/2, function ($filter) {
                    $filter->disableIdFilter();
                    $filter->equal('squad_id', trans('admin.squad'))->select(Squad::where('class_teacher','=',Admin::user()->id)->get()->pluck('name', 'id'))->load('test_id', '/api/get_squad_test');
                    $filter->where(function ($query) {
                        $query->whereHas('student', function ($query) {
                            $query->where('en_name', 'like', "%{$this->input}%");
                        });

                    }, '英文名');
                });
                $filter->column(1/2, function ($filter) {
                    $filter->equal('test_id', trans('admin.test_name'))->select(Test_config::where('status','=','0')->get()->pluck('name', 'id'));
                    $filter->equal('test_batch',trans('admin.test_batch'))->integer();
                });
            });

            $grid->actions(function ($actions) {
                $actions->disableView();
                $actions->disableDelete();
            });

            $grid->disableCreateButton();
            $grid->disableActions();
            //禁用导出按钮
            //$grid->disableExport();
            $filename="成绩单";
            $grid->exporter(new ExcelExpoter($grid,$filename));
            
            $grid->tools(function ($tools) { 
                //关闭批量删除 
                $tools->batch(function ($batch) { 
                    $batch->disableDelete(); 
                });
            });
            //$grid->disableFilter();//禁用查询
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    { 
        $form = new Form(new Student_mark());

        $form->display('id', 'ID');

        $form->select('test_id', trans('admin.test_name'))->options(Test_config::where('status','=','0')->get()->pluck('name', 'id'))->rules('required')->addElementLabelClass('must-input');
        $form->number('test_batch', trans('admin.test_batch'))->max(10)->min(1)->addElementLabelClass('must-input');
        $form->number('test_score', trans('admin.test_score'))->max(200)->min(1)->addElementLabelClass('must-input');
        
        $form->hidden('student_id')->default(request('student_id'));
            
        //去掉添加/编辑表单的重置按钮
        $form->disableReset();
        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });
        return $form;
    }
}
